package com.rune.rich;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.R.color;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	// global vars
	public static final int VISIBLE = 0x00000000;
	public static final int INVISIBLE = 0x00000004;
	public static final int GONE = 0x00000008;

	// play screen views on layout1
	private LinearLayout playScreenLay1;
	private TextView appInfo;
	private TextView disp1Num;
	private TextView disp1Diff;
	private TextView disp1Info;
	private TextView disp2Num;
	private TextView disp2Diff;
	private TextView disp2Info;
	private TextView disp3Num;
	private TextView disp3Diff;
	private TextView disp3Info;
	private TextView disp4Num;
	private TextView disp4Diff;
	private TextView disp4Info;

	// play screen views on layout2
	private LinearLayout playScreenLay2;
	private LinearLayout ron1Lay;
	private LinearLayout ron2Lay;
	private LinearLayout ron4Lay;
	private LinearLayout pan1Lay;
	private LinearLayout pan2Lay;
	private LinearLayout pan3Lay;
	private LinearLayout pan4Lay;
	private LinearLayout pan5Lay;
	private LinearLayout pan6Lay;
	private LinearLayout pan8Lay;
	private LinearLayout pan11Lay;
	private LinearLayout pan13Lay;
	private LinearLayout bu2Lay;
	private LinearLayout bu3Lay;
	private LinearLayout bu4Lay;
	private LinearLayout bu5Lay;
	private LinearLayout bu6Lay;
	private LinearLayout bu7Lay;
	private LinearLayout bu8Lay;
	private LinearLayout bu9Lay;
	private LinearLayout bu10Lay;
	private LinearLayout bu11Lay;
	private LinearLayout bu12Lay;
	private LinearLayout bu13Lay;
	// play screen vars on layout2
	private int ronTarget = -1;
	private int ronPan = 0;
	private int ronBu = 0;
	
	// use app vars
	private int initpoint = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		componentSettingMain();
	}
	
	private void componentSettingMain(){
	}

	private void componentSettingPlay(){
		playScreenLay1 = (LinearLayout)findViewById(R.id.play_screen_lay1);
		appInfo = (TextView)findViewById(R.id.app_info);
		disp1Num = (TextView)findViewById(R.id.disp1num);
		disp1Diff = (TextView)findViewById(R.id.disp1diff);
		disp1Info = (TextView)findViewById(R.id.disp1info);
		disp2Num = (TextView)findViewById(R.id.disp2num);
		disp2Diff = (TextView)findViewById(R.id.disp2diff);
		disp2Info = (TextView)findViewById(R.id.disp2info);
		disp3Num = (TextView)findViewById(R.id.disp3num);
		disp3Diff = (TextView)findViewById(R.id.disp3diff);
		disp3Info = (TextView)findViewById(R.id.disp3info);
		disp4Num = (TextView)findViewById(R.id.disp4num);
		disp4Diff = (TextView)findViewById(R.id.disp4diff);
		disp4Info = (TextView)findViewById(R.id.disp4info);
		
		playScreenLay2 = (LinearLayout)findViewById(R.id.play_screen_lay2);
		playScreenLay2.setVisibility(INVISIBLE);
		ron1Lay = (LinearLayout)findViewById(R.id.ron1lay);
		ron2Lay = (LinearLayout)findViewById(R.id.ron2lay);
		ron4Lay = (LinearLayout)findViewById(R.id.ron4lay);
		pan1Lay = (LinearLayout)findViewById(R.id.pan1lay);
		pan2Lay = (LinearLayout)findViewById(R.id.pan2lay);
		pan3Lay = (LinearLayout)findViewById(R.id.pan3lay);
		pan4Lay = (LinearLayout)findViewById(R.id.pan4lay);
		pan5Lay = (LinearLayout)findViewById(R.id.pan5lay);
		pan6Lay = (LinearLayout)findViewById(R.id.pan6lay);
		pan8Lay = (LinearLayout)findViewById(R.id.pan8lay);
		pan11Lay = (LinearLayout)findViewById(R.id.pan11lay);
		pan13Lay = (LinearLayout)findViewById(R.id.pan13lay);
		bu2Lay = (LinearLayout)findViewById(R.id.bu2lay);
		bu3Lay = (LinearLayout)findViewById(R.id.bu3lay);
		bu4Lay = (LinearLayout)findViewById(R.id.bu4lay);
		bu5Lay = (LinearLayout)findViewById(R.id.bu5lay);
		bu6Lay = (LinearLayout)findViewById(R.id.bu6lay);
		bu7Lay = (LinearLayout)findViewById(R.id.bu7lay);
		bu8Lay = (LinearLayout)findViewById(R.id.bu8lay);
		bu9Lay = (LinearLayout)findViewById(R.id.bu9lay);
		bu10Lay = (LinearLayout)findViewById(R.id.bu10lay);
		bu11Lay = (LinearLayout)findViewById(R.id.bu11lay);
		bu12Lay = (LinearLayout)findViewById(R.id.bu12lay);
		bu13Lay = (LinearLayout)findViewById(R.id.bu13lay);
	}
	
	public void mainScreenBtn(View v){
		if(v.getId() == R.id.btn_start30000){
			initpoint = 30000;
			runPlayScreen();
		}else if(v.getId() == R.id.btn_start25000){
			initpoint = 25000;
			runPlayScreen();
		}else if(v.getId() == R.id.btn_option){
			
		}
	}

	public void playScreenBtn(View v){
		if(v.getId() == R.id.btn_ron){
			playScreenLay1.setVisibility(INVISIBLE);
			playScreenLay2.setVisibility(VISIBLE);
		}else if(v.getId() == R.id.btn_tsumo){
			
		}else if(v.getId() == R.id.btn_chg_oya){
			
		}else if(v.getId() == R.id.btn_rich){
			
		}else if(v.getId() == R.id.btn_p1000){
			
		}else if(v.getId() == R.id.btn_pmod){
			
		}else if(v.getId() == R.id.btn_ron_cancel){
			ronLayClear(0);
			playScreenLay1.setVisibility(VISIBLE);
			playScreenLay2.setVisibility(INVISIBLE);
		}else if(v.getId() == R.id.btn_ron1){
			ronLayClear(1);
			ron1Lay.setBackgroundColor(Color.argb(255, 48, 16, 16));
			ronTarget = 1;
			ronRunChk();
		}else if(v.getId() == R.id.btn_ron2){
			ronLayClear(1);
			ron2Lay.setBackgroundColor(Color.argb(255, 48, 16, 16));
			ronTarget = 2;
			ronRunChk();
		}else if(v.getId() == R.id.btn_ron4){
			ronLayClear(1);
			ron4Lay.setBackgroundColor(Color.argb(255, 48, 16, 16));
			ronTarget = 4;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan1){
			ronLayClear(2);
			pan1Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 1;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan2){
			ronLayClear(2);
			pan2Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 2;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan3){
			ronLayClear(2);
			pan3Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 3;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan4){
			ronLayClear(2);
			pan4Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 4;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan5){
			ronLayClear(2);
			pan5Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 5;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan6){
			ronLayClear(2);
			pan6Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 6;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan8){
			ronLayClear(2);
			pan8Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 8;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan11){
			ronLayClear(2);
			pan11Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 11;
			ronRunChk();
		}else if(v.getId() == R.id.btn_pan13){
			ronLayClear(2);
			pan13Lay.setBackgroundColor(Color.argb(255, 16, 48, 16));
			ronPan = 13;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu2){
			ronLayClear(3);
			bu2Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 2;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu3){
			ronLayClear(3);
			bu3Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 3;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu4){
			ronLayClear(3);
			bu4Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 4;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu5){
			ronLayClear(3);
			bu5Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 5;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu6){
			ronLayClear(3);
			bu6Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 6;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu7){
			ronLayClear(3);
			bu7Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 7;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu8){
			ronLayClear(3);
			bu8Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 8;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu9){
			ronLayClear(3);
			bu9Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 9;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu10){
			ronLayClear(3);
			bu10Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 10;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu11){
			ronLayClear(3);
			bu11Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 11;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu12){
			ronLayClear(3);
			bu12Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 12;
			ronRunChk();
		}else if(v.getId() == R.id.btn_bu13){
			ronLayClear(3);
			bu13Lay.setBackgroundColor(Color.argb(255, 16, 16, 48));
			ronBu = 13;
			ronRunChk();
		}
	}
	
	private void ronLayClear(int clearType){
		if(clearType == 1){
			ron1Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ron2Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ron4Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ronTarget = -1;
		}else if(clearType == 2){
			pan1Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan2Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan3Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan4Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan5Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan6Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan8Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan11Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan13Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ronPan = 0;
		}else if(clearType == 3){
			bu2Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu3Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu4Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu5Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu6Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu7Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu8Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu9Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu10Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu11Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu12Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu13Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ronBu = 0;
		}else if(clearType == 0){
			ron1Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ron2Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ron4Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan1Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan2Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan3Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan4Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan5Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan6Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan8Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan11Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			pan13Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu2Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu3Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu4Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu5Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu6Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu7Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu8Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu9Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu10Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu11Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu12Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			bu13Lay.setBackgroundColor(Color.argb(255, 255, 255, 255));
			ronTarget = -1;
			ronPan = 0;
			ronBu = 0;
		}
	}
	
	private void ronRunChk(){
		if(ronTarget > 0 && ronPan > 0 && ronBu > 0){
			playScreenLay1.setVisibility(VISIBLE);
			playScreenLay2.setVisibility(INVISIBLE);
		}else if(ronTarget > 0 && ronPan > 4){
			playScreenLay1.setVisibility(VISIBLE);
			playScreenLay2.setVisibility(INVISIBLE);
		}
	}
	
	private void runPlayScreen(){
		setContentView(R.layout.screen_play);
		componentSettingPlay();
		
		String tmpStr = "";
		disp1Num.setText("" + initpoint);
		disp1Diff.setText("0");
		tmpStr = getPlayer(1);
		disp1Info.setText(tmpStr);
		disp2Num.setText("" + initpoint);
		disp2Diff.setText("0");
		tmpStr = getPlayer(2);
		disp2Info.setText(tmpStr);
		disp3Num.setText("" + initpoint);
		disp3Diff.setText("0");
		tmpStr = getPlayer(3);
		disp3Info.setText(tmpStr);
		disp4Num.setText("" + initpoint);
		disp4Diff.setText("0");
		tmpStr = getPlayer(4);
		disp4Info.setText(tmpStr);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	// DB access methods
	private String getPlayer(int playerNo) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://rune44.cafe24.com/rich/get_player.php?player=" + playerNo);
		try {
			HttpResponse response = httpclient.execute(httpget);
			if (response != null) {
				String line = "";
				InputStream inputstream = response.getEntity().getContent();
				line = convertStreamToString(inputstream);
				if(line.indexOf("error") > -1){
					Toast.makeText(this, "에러가 발생했습니다. 관리자에게 문의하세요.",
							Toast.LENGTH_LONG).show();
				}else{
					return line;
				}
			} else {
				Toast.makeText(this, "Unable to complete your request",
						Toast.LENGTH_LONG).show();
			}
		} catch (ClientProtocolException e) {
			Toast.makeText(this, "Caught ClientProtocolException",
					Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT)
					.show();
		} catch (Exception e) {
			Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();
		}
		return "-1";
	}

	
	
	
	
	// test
	private void testPhp() {

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://rune44.cafe24.com/rich/test.php");
		try {
			HttpResponse response = httpclient.execute(httpget);
			if (response != null) {
				String line = "";
				InputStream inputstream = response.getEntity().getContent();
				line = convertStreamToString(inputstream);
				Toast.makeText(this, "" + line,
						Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, "Unable to complete your request",
						Toast.LENGTH_LONG).show();
			}
		} catch (ClientProtocolException e) {
			Toast.makeText(this, "Caught ClientProtocolException",
					Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT)
					.show();
		} catch (Exception e) {
			Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();
		}
	}
	// help result data after DB access
	private static String convertStreamToString(InputStream is) {
		/*
		 * To convert the InputStream to String we use the
		 * BufferedReader.readLine() method. We iterate until the BufferedReader
		 * return null which means there's no more data to read. Each line will
		 * appended to a StringBuilder and returned as String.
		 */
		BufferedReader reader = new BufferedReader(new InputStreamReader(is),
				8192);
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}
}
